package cn.exrick.xboot.modules.content.serviceimpl;

import cn.exrick.xboot.common.vo.SearchVo;
import cn.exrick.xboot.modules.content.dao.HontLogDao;
import cn.exrick.xboot.modules.content.entity.HontLog;
import cn.exrick.xboot.modules.content.service.HontLogService;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 测试接口实现
 * @author Exrick
 */
@Slf4j
@Service
@Transactional
public class HontLogServiceImpl implements HontLogService {

    @Autowired
    private HontLogDao hontLogDao;

    @Override
    public HontLogDao getRepository() {
        return hontLogDao;
    }

    @Override
    public Page<HontLog> findByCondition(HontLog hontLog, SearchVo searchVo, Pageable pageable) {

        return hontLogDao.findAll(new Specification<HontLog>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<HontLog> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {

                // TODO 可添加你的其他搜索过滤条件 默认已有创建时间过滤
                Path<Date> createTimeField = root.get("createTime");
                Path<String> nameField = root.get("username");
                Path<String> nickNameField = root.get("nickName");
                List<Predicate> list = new ArrayList<Predicate>();

                if(!StringUtils.isEmpty(hontLog.getUsername())){
                    list.add(cb.or(cb.like(nickNameField,'%'+hontLog.getUsername()+'%'),cb.like(nameField,"%" + hontLog.getUsername()+"%")));
                }



                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                return null;
            }
        }, pageable);
    }

}